﻿using QT.Lib.SixLabors.ImageSharp;
using Sunny.UI;
using System.ComponentModel;

namespace ImageSpirit.Win
{
    public partial class Form_Main : UIForm2
    {
        public Form_Main()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 批处理参数
        /// </summary>
        private BatchParameters Parameters { get; set; } = new();

        /// <summary>
        /// 选择批处理图片存放文件夹
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBox_Input_ButtonClick(object sender, System.EventArgs e)
        {
            string dir = "";
            if (DirEx.SelectDirEx("请选择批处理图片存放文件夹", ref dir))
                this.textBox_Input.Text = dir;
        }

        /// <summary>
        /// 选择处理后图片存放文件夹
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBox_Output_ButtonClick(object sender, EventArgs e)
        {
            string dir = "";
            if (DirEx.SelectDirEx("请选择处理后图片存放文件夹", ref dir))
                this.textBox_Output.Text = dir;
        }

        /// <summary>
        /// 批处理设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_Settings_Click(object sender, System.EventArgs e)
        {
            FSetting fSetting = new FSetting();
            fSetting.Render();
            fSetting.ShowDialog();
            Parameters = fSetting.Parameters;
            fSetting.Dispose();
        }

        /// <summary>
        /// 批处理启动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_Run_Click(object sender, System.EventArgs e)
        {
            if (!Parameters.IsReady)
            {
                this.ShowErrorDialog2("批处理启动前请先设置批处理参数！");
                return;
            }

            Parameters.SavePath = this.textBox_Output.Text;

            List<string> imgFiles = Directory.GetFiles(this.textBox_Input.Text, "*.*", SearchOption.AllDirectories)
                                   .Where(file => file.ToLower().EndsWith(".jpg") || file.ToLower().EndsWith(".jpeg") || file.ToLower().EndsWith(".png") || file.ToLower().EndsWith(".gif") || file.ToLower().EndsWith(".bmp"))
                                   .ToList();
            Parameters.LoadPath = imgFiles;
            var imgFilesCount = imgFiles.Count();
            var processed = 0;
            double progress = 0.0;

            if (imgFilesCount == 0)
            {
                this.ShowErrorDialog2("指定批处理图片存放文件夹内图片数量为0！");
                return;
            }

            BackgroundWorker backgroundWorker = new();
            backgroundWorker.WorkerReportsProgress = true;
            backgroundWorker.DoWork += (sender, e) =>
            {
                for (int i = 0; i < Parameters.LoadPath.Count; i++)
                {
                    ImageUtilis.Filter(Parameters.LoadPath[i], Parameters.Filter, Parameters.SavePath, Parameters.Amount);
                    ImageUtilis.Merge(Parameters.LoadPath[i], Parameters.Chartlet, Parameters.CPoint, Parameters.SavePath, Parameters.Opacity);
                    ImageUtilis.Watermark(Parameters.LoadPath[i], Parameters.Text, Parameters.Font, Parameters.Color, Parameters.TPoint, Parameters.SavePath);
                    processed = i+1;
                    progress = (double)processed / imgFilesCount * 100;
                    backgroundWorker.ReportProgress((int)progress);
                }
            };
            backgroundWorker.ProgressChanged += (sender, e) =>
            {
                processBar_Batch.Value = e.ProgressPercentage;
                if (e.ProgressPercentage == 100)
                {
                    clearTempFiles();
                    this.ShowSuccessDialog2("批处理完成！");
                }
            };
            backgroundWorker.RunWorkerAsync();
        }

        /// <summary>
        /// 清除临时文件
        /// </summary>
        private void clearTempFiles() 
        {
            string[] filterFiles = Directory.GetFiles(Path.Combine(Parameters.SavePath, "Filter"));

            string[] mergeFiles = Directory.GetFiles(Path.Combine(Parameters.SavePath, "Merge"));

            foreach (string file in filterFiles.Union(mergeFiles))
            {
                File.Delete(file);
            }
        }
    }
}
